<template>
    <div class="sign">
        <div class="sign_content">
            <vue-esign id='canvas' ref="esign" :width='780' :height="450" style="border: 1px dashed #c2c1c1;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />

        </div>
        
        <div class="btn">
            <el-button type="primary" @click="save">确定</el-button>
            <el-button type="danger" @click="closes">关闭</el-button>
        </div>
        
    </div>
</template>
<script>
export default {
    name:"sign",
    props:["signKey","signKey2","data","signUrl"],
    data(){
        return{
            apiUrl:"http://localhost:8089/PPSignSDK/",
            isPolling:false,
            src:"",
            signatureImg:"",
            LastSignatureBase64Data:"",
            token:"",
            isSignStop:"N",
            pId:"",
            cId:"",
            sId:"",
            lineWidth: 6,
            lineColor: '#000000',
            bgColor: '',
            resultImg: '',
            isCrop: false

        }
    },
    beforeDestroy(){
        try{
            this.uninitDevice();
        }catch(e){}
       
        
    },
    created(){
        let ids=this.signKey.split(".");
        this.pId=ids[0];
        this.cId=ids[1];
        this.sId=this.data[this.pId][this.cId]
        this.token=sessionStorage.getItem("token");
        console.log("this.sId======"+this.sId+"=="+this.signKey2)
    },
    methods:{
        save(){
            let _this = this
            _this.$refs.esign.generate().then(res => {
                if(res){
                    this.$post("business","/api/file/fileUploadBase64",{data:res.replace("data:image/png;base64,",""),token:this.token}).then(data=>{
                        let url=data[0].url;
                        let f_id=data[0].f_id;
                        if(this.signKey2){
                            let ids=this.signKey2.split(".");
                            this.data[ids[0]][ids[1]]=url;
                        }
                        
                        sessionStorage.setItem("reloadImg","Y")
                        if(this.sId&&this.signUrl){
                            this.$post("",this.signUrl,{sId:this.sId,url:url}).then(data=>{
                                this.$emit("closes","");
                            });
                        }else{
                            this.$emit("closes","");
                        }
                    
                        
                    });
                }else{
                    this.$alert('请签名', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                        
                        }
                    });
                }
                
            }).catch(err => {
                    // 画布没有签字时提示
                
            })
           
            
        },
        closes(){
            this.$emit("closes","")
        },
        
    }
}
</script>
<style scoped>
.sign{
    height:100%;
    width:100%;
    overflow: hidden;
    display:flex;
    flex-direction: column;
}
.sign_content{
    flex:1;
}
.btn{
    text-align:center;
}
</style>